package com.rw.coco.handler.message.reply.handler.validate;

import com.rw.coco.api.data.model.BotModel;
import com.rw.coco.api.data.persistence.BotMapper;
import com.rw.coco.api.log.CocoLogger;
import com.rw.coco.api.modle.Account;
import com.rw.coco.api.modle.Bot;
import com.rw.coco.api.reply.mapper.AdvancedReply;
import com.rw.coco.handler.message.event.CocoMessageEvent;
import com.rw.coco.handler.message.reply.ReplyHandler;
import com.rw.coco.util.config.CocoCommandConfig;
import com.rw.tool.util.string.StringFormatter;
import com.rw.tool.util.string.StringUtil;

/**
 * 机器人总开关指令
 */
public class SwitchOnOFFReplyHandler implements ReplyHandler {
    @Override
    public boolean trigger(CocoMessageEvent cocoMessageEvent) {
        if (!cocoMessageEvent.hasPrefix()) {
            return false;
        }

        // 如果不是超级管理员，直接返回
        if (!cocoMessageEvent.getAccountModel().getSuperAdmin()) {
            return false;
        }

        // 开启、关闭指令
        CocoCommandConfig cocoCommandConfig = cocoMessageEvent.getCocoData().getCocoConfig().getCocoCommandConfig();
        String switchOn = cocoCommandConfig.commandProp.get(CocoCommandConfig.SWITCH_ON);
        String switchOff = cocoCommandConfig.commandProp.get(CocoCommandConfig.SWITCH_OFF);

        // 返回指令是否匹配
        return StringUtil.equalsIgnoreCase(cocoMessageEvent.getMessage(), switchOn) ||
                StringUtil.equalsIgnoreCase(cocoMessageEvent.getMessage(), switchOff);
    }

    /**
     * 这里不需要开启事务，理由同群组开关
     * 详见 GroupSwitchHandler
     */
    @Override
    public void handler(CocoMessageEvent cocoMessageEvent) {
        AdvancedReply advancedReply = cocoMessageEvent.getReplyApi().getAdvancedReply();
        // 开启、关闭指令
        CocoCommandConfig cocoCommandConfig = cocoMessageEvent.getCocoData().getCocoConfig().getCocoCommandConfig();
        String switchOn = cocoCommandConfig.commandProp.get(CocoCommandConfig.SWITCH_ON);
        String switchOff = cocoCommandConfig.commandProp.get(CocoCommandConfig.SWITCH_OFF);

        // 三大基础元素
        Bot bot = cocoMessageEvent.getBot();
        Account sender = cocoMessageEvent.getSender();
        CocoLogger cocoLogger = cocoMessageEvent.getCocoLogger();
        BotMapper botMapper = cocoMessageEvent.getPersistenceMapper().getBotMapper();
        BotModel botModel = botMapper.getBotOrDefault(cocoMessageEvent.getBot().getId());

        if (StringUtil.equalsIgnoreCase(cocoMessageEvent.getMessage(), switchOn)) {
            String switchOnLog;

            // 如果骰娘开关本就开启
            if (botModel.getSwitchOn()) {
                cocoMessageEvent.sendBack(advancedReply.botSwitchAlreadyOn(cocoMessageEvent));
                switchOnLog = "得到 {}({}) 开启骰娘指令，但 {}({}) 已经开启！";
            } else {
                botMapper.botSwitch(true);
                cocoMessageEvent.sendBack(advancedReply.botSwitchOn(cocoMessageEvent));
                switchOnLog = "得到 {}({}) 开启骰娘指令，{}({}) 开启！";
            }

            // 输出日志
            String msg = StringFormatter.format(switchOnLog, sender.getNick(), sender.getId(), bot.getName(),
                    bot.getId());
            cocoLogger.info(msg);
        } else if (StringUtil.equalsIgnoreCase(cocoMessageEvent.getMessage(), switchOff)) {
            String switchOffLog;

            if (!botModel.getSwitchOn()) {
                cocoMessageEvent.sendBack(advancedReply.botSwitchAlreadyOff(cocoMessageEvent));
                switchOffLog = "得到 {}({}) 关闭骰娘指令，但 {}({}) 已经关闭！";
            } else {
                botMapper.botSwitch(false);
                cocoMessageEvent.sendBack(advancedReply.botSwitchOff(cocoMessageEvent));
                switchOffLog = "得到 {}({}) 关闭骰娘指令，{}({}) 关闭！";
            }

            // 输出日志
            String msg = StringFormatter.format(switchOffLog, sender.getNick(), sender.getId(), bot.getName(),
                    bot.getId());
            cocoLogger.info(msg);
        }
    }
}
